package com.xijia.wuyouwallet.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.alipay.sdk.app.PayTask;
import com.xijia.wuyouwallet.MyApp;
import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.api.MyCallBack;
import com.xijia.wuyouwallet.base.BaseActivity;
import com.xijia.wuyouwallet.bean.ApliapayData;
import com.xijia.wuyouwallet.utils.MyLogger;
import com.xijia.wuyouwallet.utils.PayResult;
import com.xijia.wuyouwallet.utils.UrlConfig;
import com.xijia.wuyouwallet.utils.XUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/19 0019.
 */

public class UpgradeActivity extends BaseActivity implements View.OnClickListener{

    private ImageView upgrade_back;
    private LinearLayout upgrade_kefu,upgrade_weixin,upgrade_aplipay,upgrade_partner;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    final String orderInfo = (String) msg.obj;
                    Runnable payRunnable = new Runnable() {

                        @Override
                        public void run() {
                            PayTask alipay = new PayTask(UpgradeActivity.this);
                            Map<String, String> result = alipay.payV2(orderInfo, true);
                            //Log.e("info", result.toString());

                            Message msg = new Message();
                            msg.what = 2;
                            msg.obj = result;
                            handler.sendMessage(msg);
                        }
                    };
                    Thread payThread = new Thread(payRunnable);
                    payThread.start();
                    break;
                case 2:
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    /**
                     对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                     */
                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    String resultStatus = payResult.getResultStatus();
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                        MyApp.userData.getData().setRole("服务商");
                        MyApp.userData.getData().setRole_id("2");
                        MyApp.isRefresh = true;
                        Toast.makeText(UpgradeActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
                    } else {
                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
                        Toast.makeText(UpgradeActivity.this, "支付失败", Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upgrade);
        initView();
    }

    private void initView(){
        upgrade_back = (ImageView) findViewById(R.id.upgrade_back);
        upgrade_back.setOnClickListener(this);
        upgrade_kefu = (LinearLayout) findViewById(R.id.upgrade_kefu);
        upgrade_kefu.setOnClickListener(this);
        upgrade_weixin = (LinearLayout) findViewById(R.id.upgrade_weixin);
        upgrade_weixin.setOnClickListener(this);
        upgrade_aplipay = (LinearLayout) findViewById(R.id.upgrade_aplipay);
        upgrade_aplipay.setOnClickListener(this);
        upgrade_partner = (LinearLayout) findViewById(R.id.upgrade_partner);
        upgrade_partner.setOnClickListener(this);
    }

    /**
     * 获取支付宝订单信息
     */
    private void getAplipayOrderInfo(){
        Map<String,Object> map = new HashMap<>();
        map.put("token", MyApp.userData.getData().getToken());
        map.put("user",MyApp.userData.getData().getUser_id());
        XUtil.Post(UrlConfig.GET_APLIPAY_URL,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                MyLogger.wLog().e("获取支付宝订单信息result="+result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                ApliapayData data = JSONObject.parseObject(result,ApliapayData.class);
                if (data.getCode()==200){
                    Message message = Message.obtain();
                    message.what = 1;
                    message.obj = data.getData().getUrl();
                    handler.sendMessage(message);
                }else {
                    showToast(jsonObject.getString("msg"));
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                MyLogger.wLog().e("获取支付宝订单失败");
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.upgrade_back:
                finish();
                break;
            case R.id.upgrade_kefu:
                //showToast("正在开发中,敬请期待");
                jumpToPage(ServeActivity.class);
                break;
            case R.id.upgrade_weixin:
                showToast("正在开发中,敬请期待");
                break;
            case R.id.upgrade_aplipay:
                getAplipayOrderInfo();
                break;
            case R.id.upgrade_partner:
                showToast("正在开发中,敬请期待");
                break;
        }
    }
}
